package com.cntcdt.api.db.dao;

import java.text.SimpleDateFormat;
import java.util.List;

import com.cntcdt.api.db.dao.base.GroupDeviceDayDivideCntDaoBase;
import com.cntcdt.api.db.model.GroupDeviceDayDivideCntModel;
import com.ibm.icu.util.Calendar;

/**
 * 这是cdt_group_device_day_divide_cnt的Dao实现类
 */
public class GroupDeviceDayDivideCntDao extends GroupDeviceDayDivideCntDaoBase {
	/**
	 * GroupDeviceDayDivideCntDao对象
	 */
	private static GroupDeviceDayDivideCntDao instance = new GroupDeviceDayDivideCntDao();

	/**
	 * 默认构造方法
	 */
	private GroupDeviceDayDivideCntDao() {
	}

	/**
	 * 获得对象实例
	 */
	public static GroupDeviceDayDivideCntDao getInstance() {
		return instance;
	}

	/**
	 * 按昨天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public GroupDeviceDayDivideCntModel findByYesterday(int groupUserId, int giveBusinessIncome) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE + " WHERE addTime = ? and groupUserId = ? order by addTime";
			Object[] paramObjs1 = { yestedayDate, groupUserId };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime = ? and groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0)  order by addTime";
			Object[] paramObjs1 = { yestedayDate, groupUserId, groupUserId, groupUserId };
			paramObjs = paramObjs1;
		}

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 昨天的集团统计次数
	 * 
	 * @param groupUserId
	 * @return
	 */
	public GroupDeviceDayDivideCntModel findByYesterdayCnt(int groupUserId) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		String sql = "";
		Object[] paramObjs = null;
		sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE + " WHERE addTime = ? and groupUserId = ? order by addTime";
		Object[] paramObjs1 = { yestedayDate, groupUserId };
		paramObjs = paramObjs1;

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 查出本月的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public GroupDeviceDayDivideCntModel findByThisMonth(int groupUserId, int giveBusinessIncome) {
		Calendar cal_2 = Calendar.getInstance();
		cal_2.add(Calendar.MONTH, 0);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal_2.getTime());

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 0);
		String todayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		System.out.println(firstDay);
		System.out.println(todayDate);
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId = ? order by addTime";
			Object[] paramObjs1 = { firstDay, todayDate, groupUserId };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime >= ? and addTime <= ? and groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0)  order by addTime";
			Object[] paramObjs1 = { firstDay, todayDate, groupUserId, groupUserId, groupUserId };
			paramObjs = paramObjs1;
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 本月集团统计次数
	 * 
	 * @param groupUserId
	 * @return
	 */
	public GroupDeviceDayDivideCntModel findByThisMonthCnt(int groupUserId) {
		Calendar cal_2 = Calendar.getInstance();
		cal_2.add(Calendar.MONTH, 0);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal_2.getTime());

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 0);
		String todayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		System.out.println(firstDay);
		System.out.println(todayDate);
		String sql = "";
		Object[] paramObjs = null;

		sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId = ? order by addTime";
		Object[] paramObjs1 = { firstDay, todayDate, groupUserId };
		paramObjs = paramObjs1;
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 查出近7天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public List<GroupDeviceDayDivideCntModel> findByLastSevenDayData(int groupUserId, int giveBusinessIncome) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yesterdayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, -7);
		String lastSevenDayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar2.getTime());

		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,SUM(borrowRentSum) as borrowRentSum,SUM(sellLineMoneySum) as sellLineMoneySum,SUM(sellCDBSum) as sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId = ? GROUP BY addTime";
			Object[] paramObjs1 = { lastSevenDayDate, yesterdayDate, groupUserId };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,SUM(borrowRentSum) as borrowRentSum,SUM(sellLineMoneySum) as sellLineMoneySum,SUM(sellCDBSum) as sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime >= ? and addTime <= ? and groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) GROUP BY addTime";
			Object[] paramObjs1 = { lastSevenDayDate, yesterdayDate, groupUserId, groupUserId, groupUserId };
			paramObjs = paramObjs1;
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 近7天的集团统计次数
	 * 
	 * @param groupUserId
	 * @return
	 */
	public List<GroupDeviceDayDivideCntModel> findByLastSevenDayCntData(int groupUserId) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yesterdayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, -7);
		String lastSevenDayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar2.getTime());

		String sql = "";
		Object[] paramObjs = null;
		sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,SUM(borrowRentSum) as borrowRentSum,SUM(sellLineMoneySum) as sellLineMoneySum,SUM(sellCDBSum) as sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId = ? GROUP BY addTime";
		Object[] paramObjs1 = { lastSevenDayDate, yesterdayDate, groupUserId };
		paramObjs = paramObjs1;
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 按昨天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public GroupDeviceDayDivideCntModel findByYesterday(int groupUserId, String terminal, int giveBusinessIncome) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE + " WHERE addTime = ? and groupUserId = ? and terminal = ? order by addTime";
			Object[] paramObjs1 = { yestedayDate, groupUserId, terminal };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime = ? and groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and terminal = ? order by addTime";
			Object[] paramObjs1 = { yestedayDate, groupUserId, groupUserId, groupUserId, terminal };
			paramObjs = paramObjs1;
		}

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	public GroupDeviceDayDivideCntModel findCntByYesterday(int groupUserId, String terminal) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yestedayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		String sql = "";
		Object[] paramObjs = null;
		sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE + " WHERE addTime = ? and groupUserId = ? and terminal = ? order by addTime";
		Object[] paramObjs1 = { yestedayDate, groupUserId, terminal };
		paramObjs = paramObjs1;

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 查出本月的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public GroupDeviceDayDivideCntModel findByThisMonth(int groupUserId, String terminal, int giveBusinessIncome) {
		Calendar cal_2 = Calendar.getInstance();
		cal_2.add(Calendar.MONTH, 0);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal_2.getTime());

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 0);
		String todayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		System.out.println(firstDay);
		System.out.println(todayDate);
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime >= ? and addTime <= ? and groupUserId = ? and terminal = ? order by addTime";
			Object[] paramObjs1 = { firstDay, todayDate, groupUserId, terminal };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime >= ? and addTime <= ? and groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and terminal = ? order by addTime";
			Object[] paramObjs1 = { firstDay, todayDate, groupUserId, groupUserId, groupUserId, terminal };
			paramObjs = paramObjs1;
		}

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 查询本月的集团数据
	 * 
	 * @param groupUserId
	 * @param terminal
	 * @return
	 */
	public GroupDeviceDayDivideCntModel findByThisMonthCnt(int groupUserId, String terminal) {
		Calendar cal_2 = Calendar.getInstance();
		cal_2.add(Calendar.MONTH, 0);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal_2.getTime());

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 0);
		String todayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		System.out.println(firstDay);
		System.out.println(todayDate);
		String sql = "";
		Object[] paramObjs = null;

		sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId = ? and terminal = ? order by addTime";
		Object[] paramObjs1 = { firstDay, todayDate, groupUserId, terminal };
		paramObjs = paramObjs1;

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 查出近7天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public List<GroupDeviceDayDivideCntModel> findByLastSevenDayData(int groupUserId, String terminal,
			int giveBusinessIncome) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yesterdayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, -7);
		String lastSevenDayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar2.getTime());
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			// sql = "SELECT
			// id,terminal,name,address,addTime,updateTime,sortTime,borrowUserSum,borrowCDBSum,newUserSum,borrowRentSum,sellLineMoneySum,sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,buyCDBGiveBackCnt,buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney
			// FROM " + TABLE + " WHERE addTime >= ? and addTime <= ? and
			// groupUserId = ? and terminal = ? GROUP BY addTime";
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,SUM(borrowRentSum) as borrowRentSum,SUM(sellLineMoneySum) as sellLineMoneySum,SUM(sellCDBSum) as sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime >= ? and addTime <= ? and groupUserId = ? and terminal = ? GROUP BY addTime";
			Object[] paramObjs1 = { lastSevenDayDate, yesterdayDate, groupUserId, terminal };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			// sql = "SELECT
			// id,terminal,name,address,addTime,updateTime,sortTime,borrowUserSum,borrowCDBSum,newUserSum,borrowRentSum,sellLineMoneySum,sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,buyCDBGiveBackCnt,buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney
			// FROM " + TABLE + " WHERE addTime >= ? and addTime <= ? and
			// groupUserId in (select id from cdt_group_user where (groupId
			// in(select id from cdt_group_user where id = ? or groupId = ?) or
			// id = ?) and opStatus = 0) and terminal = ? GROUP BY addTime";
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,SUM(borrowRentSum) as borrowRentSum,SUM(sellLineMoneySum) as sellLineMoneySum,SUM(sellCDBSum) as sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE addTime >= ? and addTime <= ? and groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and terminal = ? GROUP BY addTime";

			Object[] paramObjs1 = { lastSevenDayDate, yesterdayDate, groupUserId, groupUserId, groupUserId, terminal };
			paramObjs = paramObjs1;
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 查出近7天的集团数据次数统计
	 * 
	 * @param groupUserId
	 * @param terminal
	 * @return
	 */
	public List<GroupDeviceDayDivideCntModel> findByLastSevenDayCntData(int groupUserId, String terminal) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		String yesterdayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

		Calendar calendar2 = Calendar.getInstance();
		calendar2.add(Calendar.DATE, -7);
		String lastSevenDayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar2.getTime());
		String sql = "";
		Object[] paramObjs = null;

		sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,borrowUserSum,borrowCDBSum,newUserSum,borrowRentSum,sellLineMoneySum,sellCDBSum,sellCDBNum,sellLineNum,presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,buyCDBGiveBackCnt,buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId = ? and terminal = ? GROUP BY addTime";
		Object[] paramObjs1 = { lastSevenDayDate, yesterdayDate, groupUserId, terminal };
		paramObjs = paramObjs1;

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 按昨天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public List<GroupDeviceDayDivideCntModel> findBySortTime(int groupUserId, long sortTime) {
		String sql = "SELECT * FROM " + TABLE
				+ " WHERE groupUserId = ? and sortTime < ? order by sortTime desc limit 20";
		Object[] paramObjs = { groupUserId, sortTime };
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 按昨天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public List<GroupDeviceDayDivideCntModel> findMonthDataBySortTime(int groupUserId, long sortTime,
			int giveBusinessIncome) {
		String sql = "";
		Object[] paramObjs = { groupUserId, sortTime };
		if (giveBusinessIncome == 0) {
			sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
					+ TABLE + " where groupUserId = ? and sortTime < ? group by months order by months desc limit 20";
			Object[] paramObjs1 = { groupUserId, sortTime };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
					+ TABLE
					+ " where groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and sortTime < ? group by months order by months desc limit 20";
			Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, sortTime };
			paramObjs = paramObjs1;
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 昨天的集团数据
	 * 
	 * @param groupUserId
	 * @param sortTime
	 * @param giveBusinessIncome
	 * @param queryType
	 * @return
	 */
	public List<GroupDeviceDayDivideCntModel> findMonthDataBySortTime(int groupUserId, long sortTime,
			int giveBusinessIncome, int dataType) {
		String sql = "";
		Object[] paramObjs = { groupUserId, sortTime };
		if (dataType == 2) {
			sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
					+ TABLE + " where groupUserId = ? and sortTime < ? group by months order by months desc limit 20";
			Object[] paramObjs1 = { groupUserId, sortTime };
			paramObjs = paramObjs1;
		} else if (dataType == 1) {
			if (giveBusinessIncome == 0) {
				sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
						+ TABLE
						+ " where groupUserId = ? and sortTime < ? group by months order by months desc limit 20";
				Object[] paramObjs1 = { groupUserId, sortTime };
				paramObjs = paramObjs1;
			} else if (giveBusinessIncome == 1) {
				sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
						+ TABLE
						+ " where groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and sortTime < ? group by months order by months desc limit 20";
				Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, sortTime };
				paramObjs = paramObjs1;
			}
		}

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 按昨天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public List<GroupDeviceDayDivideCntModel> findBySortTime(int groupUserId, long sortTime, String terminal,
			int giveBusinessIncome, int level) {
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "SELECT * FROM " + TABLE
					+ " WHERE groupUserId = ? and sortTime < ? and terminal = ? order by sortTime desc limit 20";
			Object[] paramObjs1 = { groupUserId, sortTime, terminal };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			// 查询所有参与 terminal=？分成的合伙人收益
			// sql = "SELECT * FROM " + TABLE + " WHERE groupUserId in (select
			// id from cdt_group_user where (groupId in(select id from
			// cdt_group_user where id = ? or groupId = ?) or id = ?) and
			// opStatus = 0) and sortTime < ? and terminal = ? order by sortTime
			// desc limit 20";
			sql = "SELECT * FROM " + TABLE
					+ " WHERE groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and sortTime < ? and terminal = ?  and level = ? order by sortTime desc limit 20";
			Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, sortTime, terminal, level };
			paramObjs = paramObjs1;
		}

		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<GroupDeviceDayDivideCntModel> findBySortTime(int groupUserId, long sortTime, String terminal,
			int giveBusinessIncome, int level, int dataType) {
		String sql = "";
		Object[] paramObjs = null;
		if (dataType == 2) {
			sql = "SELECT * FROM " + TABLE
					+ " WHERE groupUserId = ? and sortTime < ? and terminal = ? order by sortTime desc limit 20";
			Object[] paramObjs1 = { groupUserId, sortTime, terminal };
			paramObjs = paramObjs1;
		} else if (dataType == 1) {
			if (giveBusinessIncome == 0) {
				sql = "SELECT * FROM " + TABLE
						+ " WHERE groupUserId = ? and sortTime < ? and terminal = ? order by sortTime desc limit 20";
				Object[] paramObjs1 = { groupUserId, sortTime, terminal };
				paramObjs = paramObjs1;
			} else if (giveBusinessIncome == 1) {
				// sql = "SELECT * FROM " + TABLE
				// + " WHERE groupUserId in (select id from cdt_group_user where
				// (groupId in(select id from cdt_group_user where id = ? or
				// groupId = ?) or id = ?) and opStatus = 0) and sortTime < ?
				// and terminal = ? and level = ? order by sortTime desc limit
				// 20";
				// Object[] paramObjs1 = { groupUserId, groupUserId,
				// groupUserId, sortTime, terminal, level };
				sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
						+ TABLE
						+ " WHERE sortTime < ? and groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and terminal = ? GROUP BY addTime order by addTime DESC limit 20";
				Object[] paramObjs1 = { sortTime, groupUserId, groupUserId, groupUserId, terminal };
				paramObjs = paramObjs1;
			}
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}
	/**
	 * 昨天的集团数据
	 * 
	 * @param groupUserId
	 * @param sortTime
	 * @param terminal
	 * @param giveBusinessIncome
	 * @param level
	 * @param queryType
	 * @return
	 */
	// public List<GroupDeviceDayDivideCntModel> findBySortTime(int
	// groupUserId,long sortTime,String terminal,int giveBusinessIncome, int
	// level, int queryType){
	// String sql = "";
	// Object[] paramObjs = null;
	// if(queryType == 0){
	// sql = "SELECT * FROM " + TABLE + " WHERE groupUserId = ? and sortTime < ?
	// and terminal = ? order by sortTime desc limit 20";
	// Object[] paramObjs1 = {groupUserId,sortTime,terminal};
	// paramObjs = paramObjs1;
	// }else{
	// if(giveBusinessIncome == 0){
	// sql = "SELECT * FROM " + TABLE + " WHERE groupUserId = ? and sortTime < ?
	// and terminal = ? order by sortTime desc limit 20";
	// Object[] paramObjs1 = {groupUserId,sortTime,terminal};
	// paramObjs = paramObjs1;
	// }
	// else if(giveBusinessIncome == 1){
	// sql = "SELECT * FROM " + TABLE + " WHERE groupUserId in (select id from
	// cdt_group_user where (groupId in(select id from cdt_group_user where id =
	// ? or groupId = ?) or id = ?) and opStatus = 0) and sortTime < ? and
	// terminal = ? and level = ? order by sortTime desc limit 20";
	// Object[] paramObjs1 =
	// {groupUserId,groupUserId,groupUserId,sortTime,terminal,level};
	// paramObjs = paramObjs1;
	// }
	// }
	// List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql,
	// paramObjs);
	// return modelList;
	// }

	/**
	 * 按昨天的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public List<GroupDeviceDayDivideCntModel> findMonthDataBySortTime(int groupUserId, long sortTime, String terminal,
			int giveBusinessIncome) {
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
					+ TABLE
					+ " where groupUserId = ? and terminal = ? and sortTime < ? group by months order by months desc limit 20";
			Object[] paramObjs1 = { groupUserId, terminal, sortTime };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
					+ TABLE
					+ " where groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and terminal = ? and sortTime < ? group by months order by months desc limit 20";
			Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, terminal, sortTime };
			paramObjs = paramObjs1;
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<GroupDeviceDayDivideCntModel> findMonthDataBySortTime(int groupUserId, long sortTime, String terminal,
			int giveBusinessIncome, int dataType) {
		String sql = "";
		Object[] paramObjs = null;
		if (dataType == 2) {
			sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
					+ TABLE
					+ " where groupUserId = ? and terminal = ? and sortTime < ? group by months order by months desc limit 20";
			Object[] paramObjs1 = { groupUserId, terminal, sortTime };
			paramObjs = paramObjs1;
		} else if (dataType == 1) {
			if (giveBusinessIncome == 0) {
				sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
						+ TABLE
						+ " where groupUserId = ? and terminal = ? and sortTime < ? group by months order by months desc limit 20";
				Object[] paramObjs1 = { groupUserId, terminal, sortTime };
				paramObjs = paramObjs1;
			} else if (giveBusinessIncome == 1) {
				sql = "select id,terminal,name,address,addTime,updateTime,sortTime,DATE_FORMAT(addTime,'%Y%m') months,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney from "
						+ TABLE
						+ " where groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and terminal = ? and sortTime < ? group by months order by months desc limit 20";
				Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, terminal, sortTime };
				paramObjs = paramObjs1;
			}
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	// List<GroupDeviceDayDivideCntModel> findByDateTime(int groupUserId,String
	// time){
	// String sql = "SELECT * FROM " + TABLE + " WHERE groupUserId = ? and
	// addTime = ? order by sortTime desc limit 20";
	// Object[] paramObjs = {groupUserId,time};
	// List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql,
	// paramObjs);
	// return modelList;
	// }

	/**
	 * 查出本月的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public GroupDeviceDayDivideCntModel findByThisMonthNextLevel(String groupUserIds) {
		Calendar cal_2 = Calendar.getInstance();
		cal_2.add(Calendar.MONTH, 0);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal_2.getTime());

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 0);
		String todayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		System.out.println(firstDay);
		System.out.println(todayDate);
		String sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,buyCDBGiveBackCnt,buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId in (" + groupUserIds
				+ ") order by addTime";
		Object[] paramObjs = { firstDay, todayDate };
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	/**
	 * 查出本月的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public GroupDeviceDayDivideCntModel findByThisMonthNextLevel(String groupUserIds, String terminal) {
		Calendar cal_2 = Calendar.getInstance();
		cal_2.add(Calendar.MONTH, 0);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = new SimpleDateFormat("yyyy-MM-dd").format(cal_2.getTime());

		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 0);
		String todayDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		System.out.println(firstDay);
		System.out.println(todayDate);
		String sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level FROM "
				+ TABLE + " WHERE addTime >= ? and addTime <= ? and groupUserId in (" + groupUserIds
				+ ") and terminal = ? order by addTime";
		Object[] paramObjs = { firstDay, todayDate };
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	public List<GroupDeviceDayDivideCntModel> findByDataNum(int dataNum, String terminal) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -dataNum);
		String dayStr = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
		String sql = "select * from " + TABLE + " where terminal = ? and addTime >= '" + dayStr + "'";
		Object[] paramObjs = { terminal };
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	/**
	 * 查出本月的集团数据
	 * 
	 * @param id
	 *            id
	 * @return model
	 */
	public List<GroupDeviceDayDivideCntModel> findBySortTimeSum(int groupUserId, long sortTime, int giveBusinessIncome,
			int dataType) {
		String sql = "";
		Object[] paramObjs = null;
		// 如果查询借电次数
		if (dataType == 2) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE + " WHERE groupUserId = ? and sortTime < ? GROUP BY addTime order by sortTime desc";
			Object[] paramObjs1 = { groupUserId, sortTime };
			paramObjs = paramObjs1;
		}
		// 查询收益
		else if (dataType == 1) {
			if (giveBusinessIncome == 0) {
				sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
						+ TABLE + " WHERE groupUserId = ? and sortTime < ? GROUP BY addTime order by sortTime desc";
				Object[] paramObjs1 = { groupUserId, sortTime };
				paramObjs = paramObjs1;
			} else if (giveBusinessIncome == 1) {
				sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
						+ TABLE
						+ " WHERE groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and sortTime < ? GROUP BY addTime order by sortTime desc";
				Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, sortTime };
				paramObjs = paramObjs1;
			}
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<GroupDeviceDayDivideCntModel> findByDateTime(int groupUserId, String time, int giveBusinessIncome) {
		String sql = "";
		Object[] paramObjs = null;
		if (giveBusinessIncome == 0) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE + " WHERE groupUserId = ? and addTime = ? GROUP BY terminal order by sortTime desc";
			Object[] paramObjs1 = { groupUserId, time };
			paramObjs = paramObjs1;
		} else if (giveBusinessIncome == 1) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE
					+ " WHERE groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and addTime = ? GROUP BY terminal order by sortTime desc";
			Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, time };
			paramObjs = paramObjs1;
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<GroupDeviceDayDivideCntModel> findCntByDateTime(int groupUserId, String time, int giveBusinessIncome,
			int dataType) {
		String sql = "";
		Object[] paramObjs = null;
		if (dataType == 1) {
			if (giveBusinessIncome == 0) {
				sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
						+ TABLE + " WHERE groupUserId = ? and addTime = ? GROUP BY terminal order by sortTime desc";
				Object[] paramObjs1 = { groupUserId, time };
				paramObjs = paramObjs1;
			} else if (giveBusinessIncome == 1) {
				sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
						+ TABLE
						+ " WHERE groupUserId in (select id from cdt_group_user where (groupId in(select id from cdt_group_user where id = ? or groupId = ?) or id = ?) and opStatus = 0) and addTime = ? GROUP BY terminal order by sortTime desc";
				Object[] paramObjs1 = { groupUserId, groupUserId, groupUserId, time };
				paramObjs = paramObjs1;
			}
		} else if (dataType == 2) {
			sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
					+ TABLE + " WHERE groupUserId = ? and addTime = ? GROUP BY terminal order by sortTime desc";
			Object[] paramObjs1 = { groupUserId, time };
			paramObjs = paramObjs1;
		}
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public GroupDeviceDayDivideCntModel findByDateTimeAndGroupUserId(String dateTime, int groupUserId,
			String terminal) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal_2 = Calendar.getInstance();
		cal_2.add(Calendar.MONTH, 0);
		cal_2.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
		String firstDay = format.format(cal_2.getTime());
		String sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level FROM "
				+ TABLE + " WHERE groupUserId = ? and addTime >= '" + firstDay
				+ "' and addTime <= ? and terminal = ? order by sortTime desc";
		Object[] paramObjs = { groupUserId, dateTime, terminal };
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		if (modelList.size() > 0) {
			return modelList.get(0);
		}
		return null;
	}

	public List<GroupDeviceDayDivideCntModel> findByDate(int groupUserId, String startDate, String endDate) {
		String sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE
				+ " WHERE groupUserId = ? and addTime >=? and addTime<= ? GROUP BY addTime order by sortTime desc";
		Object[] paramObjs = { groupUserId, startDate, endDate };
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

	public List<GroupDeviceDayDivideCntModel> findSingleDeviceByDate(int groupUserId, String terminal, String startDate,
			String endDate) {
		String sql = "SELECT id,terminal,name,address,addTime,updateTime,sortTime,sum(borrowUserSum) as borrowUserSum,sum(borrowCDBSum) as borrowCDBSum,sum(newUserSum) as newUserSum,sum(borrowRentSum) as borrowRentSum,sum(sellLineMoneySum) as  sellLineMoneySum,sum(sellCDBSum) as sellCDBSum,sum(sellCDBNum) as sellCDBNum,sum(sellLineNum) as sellLineNum,sum(presentLineNum) as presentLineNum,groupUserId,higherUpGroupUserId,residueBorrowRentSum,residueSellLineMoneySum,residueSellCDBSum,level,sum(buyCDBGiveBackCnt) as buyCDBGiveBackCnt,sum(buyCDBGiveBackMoney) as buyCDBGiveBackMoney,residueBuyCDBGiveBackMoney FROM "
				+ TABLE
				+ " WHERE groupUserId = ? and terminal = ? and addTime >=? and addTime<= ? GROUP BY addTime order by sortTime desc";
		Object[] paramObjs = { groupUserId, terminal, startDate, endDate };
		List<GroupDeviceDayDivideCntModel> modelList = this.queryModelList(sql, paramObjs);
		return modelList;
	}

}
